/*
 * hd44780-sreg.c
 *
 *  Created on: Sep 27, 2009
 *      Author: Orlando Arias
 *	   License: GPLv3
 *
 *   libhd44780 for Arduino Systems
 *   Copyright (C) 2009  Orlando Arias
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <inttypes.h>
#include "WProgram.h"
#include "private-sreg.h"
#include "common/common_private.h"

#include "hd44780-sreg.h"

void lcds_init(sLCD *l){
	// Set up pinout
	pinMode(l->clock, OUTPUT);
	digitalWrite(l->clock, LOW);
	pinMode(l->strobe, OUTPUT);
	digitalWrite(l->strobe, LOW);
	pinMode(l->datapin, OUTPUT);
	digitalWrite(l->datapin, LOW);


	// Initialize LCD
	delay(45);					// Wait for more than 40ms
	lcds_SIPO(l, 0x6);			// Function set
	lcd_triggerStrobe(l->strobe);
	delay(5);					// Wait for more than 4.1 ms
	lcds_SIPO(l, 0x6);			// Function set
	lcd_triggerStrobe(l->strobe);
	delayMicroseconds(200);		// Wait for more than 100 us
	lcds_SIPO(l, 0x6);			// Function set
	lcd_triggerStrobe(l->strobe);
	delayMicroseconds(200);
	lcds_SIPO(l, 0x4);			// Initialize in 4bit mode
	lcd_triggerStrobe(l->strobe);
	delayMicroseconds(200);

	lcds_sendCommand(l, 0x28);	// Function set instruction
	lcds_sendCommand(l, BLANK_DISPLAY);
	lcds_sendCommand(l, CLEAR);
	delay(2);					// TODO: Remove when sendCommand() has been fixed
	lcds_sendCommand(l, EM_INCREMENT_OFF);
	lcds_sendCommand(l, CURSOR_INVISIBLE);
}

void lcds_clear(sLCD *l){
	lcds_sendCommand(l, CLEAR);
}

void lcds_sendChar(sLCD *l, uint8_t chr){
	lcds_sendRaw(l, chr, HIGH);		// RS = 1, write to DDRAM
	//TODO: Add some finely tuned delay
	delay(1);
}
void lcds_sendCommand(sLCD *l, command cmd){
	lcds_sendRaw(l, cmd, LOW);		// RS = 0, write command
	//TODO: Add some finely tuned delay
	delay(1);
}

void lcds_sendStr(sLCD *l, const char str[]){
	const char *p = str;
	while (*p)
		lcds_sendChar(l, *(p++));
}

void lcds_setAC(sLCD *l, uint8_t row, uint8_t col){
	/*
	 *  Positions address counter in terms of row/column
	 *
	 *  NOTICE: Function assumes most standard layouts. Refer
	 *  to datasheet for details. If your LCD uses a different
	 *  screen layout, you may implement your own AC function
	 *  by using the call:
	 *  lcd4_sendCommand(l, SET_POSITION | offset);
	 */
	uint8_t row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
	lcds_sendCommand(l, SET_POSITION | (col + row_offsets[row]));
}

void lcds_writeCGRAM(sLCD *l, uint8_t location, uint8_t data[]){
	/*
	 *  Consult the documentation on how to construct the
	 *  data[] array
	 *
	 *  NOTICE: Unlike the 8bit and the 4bit implementation, the
	 *  sreg implementation cannot read from the LCD, thus the
	 *  AC is not automatically reset to the previous position.
	 */
	if (location > 7) return;	// if invalid location, exit function
	lcds_sendCommand(l, 0x40 | (location<<3));	// set CGRAM location
	// write and commit data
	for (uint8_t i = 0; i<8; i++){
		lcds_SIPO(l, (data[i]>>3) | 0x1);
		lcd_triggerStrobe(l->strobe);
		lcds_SIPO(l, (data[i]<<1) | 0x1);
		lcd_triggerStrobe(l->strobe);
	}
}
